package com.wenwu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsMapping;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsMappingMapper;
import com.wenwu.service.MappingService;

/**
 * @project RelicsProject-service
 * @class HumanityServiceImpl.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年5月6日 下午10:58:37
 * @description 人文环境管理模块
 */
@Service
public class MappingServiceImpl implements MappingService {
	@Autowired
	private RelicsMappingMapper mappingMapper;
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;
	public String addObj(RelicsMapping mapping)
			throws RelicsException {
		int result = 0;
		try {
			List<RelicsAttachment> list = mapping.getFileList();
			if (mapping.getMappingId()==null) {
				result += mappingMapper.insertSelective(mapping);
				if (mapping.getMappingId() > 0) {
					if (list != null && list.size() > 0) {
						for (RelicsAttachment attachment : list) {
							attachment.setAttachmentDesc("测绘图纸附件");
							attachment.setTableOwner(mapping.getMappingId());// 外键
						}
						result += attachmentMapper.insertList(list);
					}
				}
				if (result > 0) {
					return "测绘图纸添加成功";
				} else {
					return "测绘图纸添加失败";
				}
			} else {
				result += attachmentMapper
						.deleteByKey(mapping.getMappingId(),mapping.getOwnList());// 先删除原有的附件，再添加
				if (list != null && list.size() > 0) {
					for (RelicsAttachment attachment : list) {
						attachment.setAttachmentDesc("测绘图纸附件");
						attachment.setTableOwner(mapping.getMappingId());// 外键
					}
					result += attachmentMapper.insertList(list);
				}
				result += mappingMapper.updateByPrimaryKeySelective(mapping);
				if (result > 0) {
					return "测绘图纸修改成功";
				} else {
					return "测绘图纸修改失败";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RelicsException("系统错误，操作失败");
		}
	}
	
	
	public RelicsMapping selectObjByKey(Integer basicId)
			throws RelicsException {
		try {
			return mappingMapper.selectObjByKey(basicId);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
	public PaginationVO selectList(RelicsMapping mapping, PaginationVO pageVo)
			throws RelicsException {
		try {
			pageVo.setTotalRecord(mappingMapper.selectTotalRecord(mapping));
			pageVo.setList(mappingMapper.selectList(mapping, pageVo));
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
		return pageVo;
	}
	public int deleteByKey(List<?> keys) throws RelicsException {
		try {
			return mappingMapper.deleteByKey(keys);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
	public RelicsMapping selectThisObjByKey(Integer mappingId)
			throws RelicsException {
		try {
			return mappingMapper.selectByPrimaryKey(mappingId);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
}
